# 定义字典
d = {"name":"张三","age":20,"sex":"男"}
print(d)
d1 = dict(name="张三",age=20,sex="男")
print(d1)
# 增加
d["grade"] = "人工智能1班"
print(d)
d.update(house=403)
print(d)
# 查找
print("grade" in d)
print(d.get("grade"))
# 修改 和新增一致
d["house"] = 404
print(d)
d.update(name="李四")
print(d)
# 删除
print(d.pop("house"))
print(d)
print(d.popitem())
print(d)
del d["age"]
print(d)
# 清空
d.clear()
print(d)
# 遍历
print(d1.items())
for i in d1.items():
    k,v = i
    print(k,v)
# keys键
for i in d1.keys():
    print(i)
# values 值
for i in d1.values():
    print(i)
# 排序
d2 = {"a":10,"d":9,"c":21,"b":15}
print(dict(sorted(d2.items(),reverse=True,key=lambda x:x[0])))